package com.yanqu.road.server.manger.config.grandchild.skill;

import com.yanqu.road.entity.grandchild.GrandChildData;
import com.yanqu.road.server.manger.config.grandchild.skill.enums.eGrandChildSkillCondition;
import com.yanqu.road.server.manger.config.grandchild.skill.enums.eGrandChildSkillFlag;
import com.yanqu.road.server.manger.config.grandchild.skill.param.GrandChildSkillConditionParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Objects;

public class GrandChildSkillUtils {

    private static final Logger logger = LogManager.getLogger(GrandChildSkillUtils.class);

    /**
     * 前提随机条件
     *
     * @return 是否满足条件
     */
    public static boolean canRandom(GrandChildData data, String condition) {
        GrandChildSkillConditionParam param = getConditionParam(condition);
        return eGrandChildSkillCondition.canRandom(data, param);
    }

    // 获取随机参数
    private static GrandChildSkillConditionParam getConditionParam(String condition) {

        // 是否无条件
        if (Objects.equals(condition, "0")) {
            return new GrandChildSkillConditionParam("NOCOND", eGrandChildSkillFlag.EQ, 0);
        }

        char[] chars = condition.toCharArray();

        // 【1. 找出前几个字母】
        StringBuilder sb1 = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            if (!(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')) {
                break;
            }
            sb1.append(ch);
        }
        String name = sb1.toString();

        // 【2. 找出中间几个运算符号】
        StringBuilder sb2 = new StringBuilder();
        for (int i = sb1.length(); i < chars.length; i++) {
            char ch = chars[i];
            if (ch >= '0' && ch <= '9') {
                break;
            }
            sb2.append(ch);
        }
        eGrandChildSkillFlag flag = eGrandChildSkillFlag.getByName(sb2.toString());

        // 【3. 最后是数字值】
        StringBuilder sb3 = new StringBuilder();
        for (int i = sb1.length() + sb2.length(); i < chars.length; i++) {
            sb3.append(chars[i]);
        }
        int value = Integer.valueOf(sb3.toString());

        // 【返回】
        return new GrandChildSkillConditionParam(name, flag, value);
    }

}
